export default {
	data() {
		return {
			userId:'',
			nickname: '',
			password: '',
			passwords: '',
			phone:'',
			//是否显示加载圈
			show: false,
			//是否显示弹出框
			show1: false,
			finishTip: '',
			src: ' '
		}
	},
	onLoad() {

	},
	methods: {
		//注册
		enroll() {
			if (this.password != this.passwords) {
				uni.showToast({
					title: '两次输入的密码不同',
					icon:'none',
					duration: 2000
				});
				this.show1 = true;
				this.show = false;
				return;
			}
			this.show = true;
			this.$request('/User/insertUser', {
				// 传参参数名：参数值,如果没有，就不需要传
				"nickname": this.nickname,
				"password": this.password,
				"headImg": this.src,
				"phone":this.phone

			}).then(res => {

				if (res.code == 200) {
					uni.showToast({
						title: '注册成功',
						icon:'none',
						duration: 2000
					});
					this.finishTip = "账号：" + res.data;
					this.userId = res.data;
					this.show1 = true;
					this.show = false;
				} else {
					uni.showToast({
						title: res.msg,
						icon:'none',
						duration: 2000
					});
					this.show1 = true;
					this.show = false;
				}
			})
		},
		//上传图片
		upload() {
			uni.chooseImage({
				success: (chooseImageRes) => {
					const tempFilePaths = chooseImageRes.tempFilePaths;
					const uploadTask = uni.uploadFile({
						url: this.$url + '/Upload/uploadFile',
						filePath: tempFilePaths[0],
						name: 'file',
						formData: {
							'type': 'head'
						},
						success: (uploadFileRes) => {
							var data = JSON.parse(uploadFileRes.data)
							if (data.code == 200) {
								this.src = data.data.url;
								uni.showToast({
									title: '图片上传成功',
									icon:'none',
									duration: 2000
								});
							} else {
								uni.showToast({
									title: '图片上传出错',
									icon:'none',
									duration: 2000
								});
							}
						}
					});
				}
			});
		},
		//返回登录页面
		returnLogin() {
			uni.navigateTo({
				url: '/pages/login/login',
			});
		},
		//登录
		login() {
			this.$request('/User/login', {
				// 传参参数名：参数值,如果没有，就不需要传
				"id": this.userId,
				"password": this.password
			}).then(res => {
		
				if (res.code == 200) {
					//缓存个人信息
					uni.setStorage({
						key: 'userId',
						data: res.data.id,
						success: function() {
		
						}
					});
					uni.setStorage({
						key: 'token',
						data: res.msg.split("|token:")[1],
						success: function() {
		
						}
					});
					uni.setStorage({
						key: 'headImg',
						data: res.data.headImg,
						success: function() {
		
						}
					});
					uni.setStorage({
						key: 'nickname',
						data: res.data.nickname,
						success: function() {
		
						}
					});
					uni.reLaunch({
						url: '/pages/index/index',
					});
				} else {
					uni.showToast({
						title: '登录失败',
						icon:'none',
						duration: 2000
					});
				}
			})
		},
	}
}
